package com.example.poemtest.index.util;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
 * 飞花令或诗词接龙
 */
public class TextViewUtils {
    public static void setTextColor(TextView textView, String text, int color) {
        SpannableString spannableString = new SpannableString(textView.getText().toString());
        int startIndex = textView.getText().toString().indexOf(text);
        //特殊化处理
        if (textView.getText().toString().contains("飞花令")){
            startIndex = textView.getText().toString().lastIndexOf(text);
        }
        if (startIndex >= 0) {
            spannableString.setSpan(new ForegroundColorSpan(color), startIndex, startIndex + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
        }
    }
}
